(async () => {

let width = 5;
let height = 5;


await loadSizes();
checkInstance();



async function loadSizes(){
    let values = await browser.storage.local.get();
    if (values.width)
        width = values.width;
    else{
        width = 50;
        browser.storage.local.set({"width": 50});
    }
    if (values.height)
        height = values.height;
    else{
        height = 50;
        browser.storage.local.set({"height": 50});
    }
}


function setWH(sheetText){
    console.log("3-1", width, height);
    for (var i = 0; i < document.styleSheets.length; i++) {
        try {
            var theRules = new Array();
            if (document.styleSheets[i].cssRules)
                theRules = document.styleSheets[i].cssRules;
            else if ( document.styleSheets[i].rules )
                theRules = document.styleSheets[i].rules;
            for (var rule of theRules) {
                if (rule.cssText.search(sheetText) != -1 ) {
                    rule.style.width = width + "px";
                    rule.style.height = height + "px";
                    // console.log("2-2",rule.cssText);
                }
            }
        } catch (e) {
            console.log("ERROR ", e.message);
        }
    }
}


function checkInstance(){
    console.log("2-1", width, height);
    try{
        if (document.getElementById("mastodon")) {
            console.log("Mastodon detected");
            setWH(".reply-indicator__content .emojione, .status__content .emojione");
        }
    }
    catch(e){
        console.log("ERROR ", e);
    }
    try{
        if (document.getElementsByTagName("noscript")[0].textContent === "To use Pleroma, please enable JavaScript." ) {
            console.log("Pleroma detected");
            setWH(".RichContent .emoji");
        }
    }
    catch (e) {
        console.log("ERROR ", e);
    }
}


browser.storage.local.onChanged.addListener(async()=>{
    await loadSizes();
    checkInstance();
})

})();